# encoding= utf-8
#!/usr/sbin
import sys
sys.path.append("/root/xiao/project")
from excelword.readexcel.readexcel import ReadExcel
from excelword.operate.oprxls import operatexls
from excelword.operate.login import login
from excelword.operate.logout import logout
from excelword.operate.writedata import writexls
from excelword.util.isok import okTest
from excelword.util.sendmail import send_mail
import time
if __name__ == '__main__':
    filepath = "./exceldoc/proute.xls"
    readexl = ReadExcel(filepath)
    #查行数
    nline = readexl.readline()
    #查列表中的数据
    # print(readexl.readdata(2,'descrption'))
    header = login()  # 登陆拿到带有token 的header
    for index in range(1,nline):
        #将读取到的数据传入动作类，获取返回结果,调用之前需要进行登陆
        # 传入参数包含: url 信息，请求动作，请求body
        url = readexl.readdata(index, 'url')
        methods = readexl.readdata(index, 'method')
        data = readexl.readdata(index, 'body')
        descrption = readexl.readdata(index, 'descrption')
        #读取期望结果
        exceptresult =readexl.readdata(index,'exceptres')
        res = operatexls().operation(url, methods, data, header=header)
        print("本次操作 "+str(descrption)+"的结果为:" + str(res))
        #将返回结果写入
        writexls(filepath,index,'realres',res)
        #将是否通过写入,期望值与返回值进行比较，并将比较结果写入
        # 传入index 方便统计失败案例
        testres = okTest().oktest(index,exceptresult,res)
        filecontent = "第"+str(index)+"条case:"+str(descrption)+"测试结果为:"+str(testres)
        with open('./rundata/rundata.log', 'a', encoding='utf-8') as f:
            f.write(time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())+"  ")
            f.write(filecontent)
            f.write("\n")
        writexls(filepath, index, 'isok', testres)
    res = logout()
    print(res)
#通过okTest() 模块拿到passlist列表和faillist 列表
#发送邮件
send_mail(okTest.pass_list,okTest.fail_list)

